package com.zhongmai.yzfgrabanorder.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhongmai.yzfgrabanorder.R;
import com.zhongmai.yzfgrabanorder.base.BaseActivity;
import com.zhongmai.yzfgrabanorder.netty_lib.NettyService;
import com.zhongmai.yzfgrabanorder.netty_lib.netty.NettyClient;
import com.zhongmai.yzfgrabanorder.utils.Constants;
import com.zhongmai.yzfgrabanorder.utils.LogUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 项目名称：YZFGrabAnOrder
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2020/9/24 13:49
 * 修改人：Administrator
 * 修改时间：2020/9/24 13:49
 * 修改备注：
 */
public abstract class BaseFragment extends Fragment {
    private Unbinder unbinder;
    protected BaseActivity myActivity;
    protected TextView mTV_title;
    public Intent intent = new Intent(MyApplication.getContext(), NettyService.class);
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myActivity = (BaseActivity) getActivity();
        mTV_title = getActivity().findViewById(R.id.mTV_title);
        loadingData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int layout = createSuccessView();
        View view = inflater.inflate(layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        changeLayout();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        logMag("Fragment-------onStart");
        againLoad();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        try {
            if (getUserVisibleHint()) {//界面可见时
                switchFragemnt();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 启动长连接服务
     */
    protected void loadingLink() {
        if (!NettyClient.isConnect) {
            getActivity().startService(intent);
        }
    }

    /**
     * 解绑长连接服务
     */
    protected void breakLongLink() {
        if (NettyClient.isConnect) {
            NettyClient.getInstance().disconnect();
            logMag("ActivityA 执行 unbindService");
            getActivity().stopService(intent);
        }
    }



    public void showToast(String message) {
        myActivity.showToast(message);
    }

    protected abstract int createSuccessView();

    protected abstract void changeLayout();

    protected abstract void loadingData();

    protected abstract void againLoad();//重新加载

    protected abstract void switchFragemnt();//重新显示

    protected void logMag(String msg) {
        LogUtils.bugMes(msg,5);
    }
    protected void debug(String msg) {
        LogUtils.debug(msg,5);
    }
    //    public abstract void initView();
}
